package com.bdc.whatwhat.views.cell;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.adapters.NotificationAdapter;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.AjouterAmisTask;
import com.bdc.whatwhat.utils.transform.CircleTransform;
import com.bdc.whatwhat.utils.transform.CropTransformation;
import com.facebook.Session;
import com.squareup.picasso.Picasso;

/**
 * Created by kevin on 21/06/14.
 */
public class DemandeAmiCell extends ViewCell<Whatter> implements View.OnClickListener, AjouterAmisTask.AjouterAmisTaskTaskDelegate {

    ImageView image;
    TextView prenom;
    TextView nom;
    TextView status;
    View accepter;
    View refuser;

    NotificationAdapter.NotificationAdapterDelegate notificationAdapterDelegate;

    @Override
    public void animer() {

    }

    @Override
    public void construire() {
        if (getParams() != null && getParams().length >= 1) {
            Object param0 = getParams()[0];
            if (param0 instanceof NotificationAdapter.NotificationAdapterDelegate)
                notificationAdapterDelegate = (NotificationAdapter.NotificationAdapterDelegate) param0;
        }
    }

    @Override
    public void charger() {
        this.image = (ImageView) findViewById(R.id.imageWatter);
        this.prenom = (TextView) findViewById(R.id.prenom);
        this.nom = (TextView) findViewById(R.id.nom);
        this.status = (TextView) findViewById(R.id.status);

        this.accepter =  findViewById(R.id.accept);
        this.refuser =  findViewById(R.id.refuse);
    }

    @Override
    public void remplir() {

        if (getObject() != null) {


            if (getObject().getPrenom() != null)
                this.prenom.setText(getObject().getPrenom());

            if (getObject().getNom() != null)
                this.nom.setText(getObject().getNom());

            if (getObject().getWhatWhat() != null && getObject().getWhatWhat().getTexte() != null)
                this.status.setText("\""+getObject().getWhatWhat().getTexte()+ " \"");
            else
                this.status.setText("Ne fais rien");

            if (getObject().getUrlImage() != null && !getObject().getUrlImage().trim().equalsIgnoreCase("")) {
                Picasso.with(context).load(getObject().getUrlImage()).transform(new CropTransformation(300)).transform(new CircleTransform()).into(image);
            } else {
                Log.e("Picasso", "error");
            }

        }

    }

    @Override
    public void ajouterListeners() {
        getView().setOnClickListener(this);

        accepter.setOnClickListener(this);
        refuser.setOnClickListener(this);

        findViewById(R.id.cell_content).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        boolean accept = true;

        if(view.getId() == R.id.refuse)
            accept = false;

        if(accept){
            SessionManager sessionManager = new SessionManager(getContext());
            new AjouterAmisTask(this).execute(sessionManager.getToken(), getObject().getId(), new Boolean(accept).toString());
        }

    }

    @Override
    public void afficherChargement(boolean afficher) {

    }

    @Override
    public void onAmisAjouter() {
        if(notificationAdapterDelegate != null)
            notificationAdapterDelegate.supprimer(getPosition());
    }

    @Override
    public void onErreurReseau() {

    }
}
